package com.autosalon.tools;

import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.themes.ChameleonTheme;

public class PagingComponent extends Panel {
	

    private ComboBox pagingChooser;
    private Label pagingLabel;
    private Button pagingControlLeft;
    private Button pagingControlRight;
    private int from;
    private int to;
    private final IPaging outputRederTable;
    
    
    public PagingComponent(IPaging output){
    	this.outputRederTable = output;
    	init();
    }

	public void init(){
        pagingChooser = new ComboBox();
    	pagingChooser.setWidth("50px");
    	pagingChooser.addItem(10);
    	pagingChooser.addItem(25);
    	pagingChooser.addItem(50);
    	pagingChooser.setValue(10);
    	pagingChooser.setFilteringMode(Filtering.FILTERINGMODE_OFF);
    	pagingChooser.setImmediate(true);
    	pagingChooser.addListener(new Property.ValueChangeListener() {

			@Override
			public void valueChange(ValueChangeEvent event) {
				from = 0;
				to=(from+Integer.parseInt(pagingChooser.getValue().toString()));
				pagingLabel.setValue(from+"..."+to);
				outputRederTable.naplnTabulkuObjednavky();
			}
		});
    	from = 0;
    	to = 10;
    	pagingLabel = new Label();
    	pagingLabel.setValue(from+"..."+pagingChooser.getValue());
    	pagingControlLeft = new Button("<<");
    	pagingControlRight = new Button(">>");
    	pagingControlLeft.setStyleName(ChameleonTheme.BUTTON_BORDERLESS);
    	pagingControlRight.setStyleName(ChameleonTheme.BUTTON_BORDERLESS);
    	
    	pagingControlLeft.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				if((from-Integer.parseInt(pagingChooser.getValue().toString()))<=0){
					from = 0;
				}else{
					from = from-Integer.parseInt(pagingChooser.getValue().toString());
				}
				to=(from+Integer.parseInt(pagingChooser.getValue().toString()));
				pagingLabel.setValue(from+"..."+to);
				outputRederTable.naplnTabulkuObjednavky();
			}
		});
    	
    	pagingControlRight.addListener(new Button.ClickListener() {
			
			@Override
			public void buttonClick(ClickEvent event) {
				from = from+Integer.parseInt(pagingChooser.getValue().toString());
				to=(from+Integer.parseInt(pagingChooser.getValue().toString()));
				pagingLabel.setValue(from+"..."+to);
				outputRederTable.naplnTabulkuObjednavky();
			}
		});
    	
    	HorizontalLayout paginglayout = new HorizontalLayout();
        paginglayout.addComponent(pagingChooser);
        paginglayout.addComponent(pagingControlLeft);
        paginglayout.addComponent(pagingLabel);
        paginglayout.addComponent(pagingControlRight);
        this.addComponent(paginglayout);
	}

	public ComboBox getPagingChooser() {
		return pagingChooser;
	}

	public void setPagingChooser(ComboBox pagingChooser) {
		this.pagingChooser = pagingChooser;
	}

	public int getPagingConstant() {
		return from;
	}

	public void setPagingConstant(int pagingConstant) {
		this.from = pagingConstant;
	}

	public int getFrom() {
		return from;
	}

	public void setFrom(int from) {
		this.from = from;
	}

	public int getTo() {
		return to;
	}

	public void setTo(int to) {
		this.to = to;
	}
	
	

}
